package com.laguille.games.tetris.rules;

import java.util.ArrayList;
import java.util.List;

import com.laguille.games.tetris.model.Field;
import com.laguille.games.tetris.model.Tetromino.Coordinates;
import com.laguille.games.tetris.model.Tetromino.TetrominoBlock;
import com.laguille.games.tetris.model.Tetromino.TetrominoPiece;

public class RuleEngine {

	private static RuleEngine ruleEngine;
	
	private RuleEngine() {}
	
	public static RuleEngine getInstance()
	{
		return ruleEngine;
	}
	public static boolean isPositionValid(Field field, TetrominoPiece piece)
	{
		for (TetrominoBlock b : piece.getBlocks())
		{
			Coordinates c = b.getCoordinates();
			if ( field.isOutOfBounds(c) || field.isOccupied(c))
			{
				System.out.println("POSITION NON VALID: " + c);
				return false;
			}
		}
		return true;
	}
	public static boolean isPositionValid(Field field, TetrominoPiece piece, Coordinates...newCoordinates)
	{
		List<Coordinates> currentCoordinates = new ArrayList<Coordinates>(4);
		for (TetrominoBlock b : piece.getBlocks())
		{
			currentCoordinates.add(b.getCoordinates());
		}
		
		for (Coordinates c : newCoordinates)
		{
			if (	field.isOutOfBounds(c)
				||	(field.isOccupied(c) && !currentCoordinates.contains(c))) // if the field is occupied by another piece
			{
				System.out.println("POSITION NON VALID: " + c);
				return false;
			}
		}
		return true;
	}

}
